//
//  THCPCObjectView.m
//  YJB
//
//  Created by 中电大维 on 2016/12/30.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THCPCObjectView.h"

@implementation THCPCObjectView
{
    UILabel     *_methodLabel;
    UIButton    *_fixedButton;
    UIButton    *_dynamicButton;
//    UILabel     *_fixAlertLabel;
//    UILabel     *_dynamicAlertLabel;
    UILabel     *_answerLabel;
    UILabel     *_answerRequireLabel;
    UIView      *_alphaView;
    UIButton    *_helpButton;
}

- (instancetype)init
{
    if (self = [super init]) {
        
        _methodLabel = [[UILabel alloc] init];
        _fixedButton = [[UIButton alloc] init];
        _dynamicButton = [[UIButton alloc] init];
//        _fixAlertLabel = [[UILabel alloc] init];
//        _dynamicAlertLabel = [[UILabel alloc] init];
        _answerLabel = [[UILabel alloc] init];
        _answerRequireLabel = [[UILabel alloc] init];
        self.methodState = NO;
        _answerField = [[UITextField alloc] init];
        _answerRequireView = [[UITextView alloc] init];
        _helpButton = [[UIButton alloc] init];
        
        _dynamicButton.tag = 1;
        _fixedButton.tag = 2;
        [_dynamicButton addTarget:self action:@selector(fixOperttionClick:) forControlEvents:UIControlEventTouchUpInside];
        [_fixedButton addTarget:self action:@selector(fixOperttionClick:) forControlEvents:UIControlEventTouchUpInside];
        [_helpButton setImage:Image(@"doubt_icon") forState:UIControlStateNormal];
        [_helpButton addTarget:self action:@selector(helpClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_methodLabel];
        [self addSubview:_fixedButton];
        [self addSubview:_dynamicButton];
        [self addSubview:_helpButton];
//        [self addSubview:_dynamicAlertLabel];
//        [self addSubview:_fixAlertLabel];
        [self addSubview:_answerLabel];
        [self addSubview:_answerRequireLabel];
        [self addSubview:_answerField];
        [self addSubview:_answerRequireView];
        
        [self setfont];
        [self setcolor];
        [self settext];
        [self setframe];
        
        [self test];
        
        self.backgroundColor = [UIColor whiteColor];
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)helpClick:(UIButton *)button
{
    self.alphaView.hidden = NO;
}
- (void)hiddenAlphaView
{
    self.alphaView.hidden = YES;
}
- (void)setMethodState:(BOOL)methodState
{
    if (methodState) {//固定匹配
        _dynamicButton.selected = NO;
        _fixedButton.selected = YES;
    }else{//动态匹配
        _dynamicButton.selected = YES;
        _fixedButton.selected = NO;
    }
    _methodState = methodState;
}

- (void)fixOperttionClick:(UIButton *)button
{
    switch (button.tag) {
        case 1:
        {
            self.methodState = NO;
        }
            break;
        case 2:
        {
            self.methodState = YES;
        }
            break;
    }
}

- (void)test{
//    _methodLabel.backgroundColor = [UIColor whiteColor];
//    _dynamicButton.backgroundColor = [UIColor blueColor];
//    _fixedButton.backgroundColor = [UIColor blueColor];
}

- (void)setframe{
    
    CGFloat width = [NSString widthForString:_methodLabel.text fontSize:16 andHeight:15];
    [_methodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(16);
        make.left.mas_equalTo(12);
        make.width.mas_equalTo(width+6);
        make.height.mas_equalTo(15);
    }];
    [_dynamicButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_methodLabel.mas_centerY);
        make.left.equalTo(_methodLabel.mas_right).offset(15);
        make.width.mas_equalTo(width+22);
        make.height.mas_equalTo(16);
    }];
    [_fixedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_methodLabel.mas_centerY);
        make.left.equalTo(_dynamicButton.mas_right).offset(15);
        make.width.mas_equalTo(width+22);
        make.height.mas_equalTo(16);
    }];
    
    [_helpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_methodLabel.mas_centerY);
        make.left.equalTo(_fixedButton.mas_right).offset(0);
        make.height.equalTo(_fixedButton.mas_height);
        make.width.equalTo(_fixedButton.mas_height);
    }];
    [_answerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_methodLabel.mas_bottom).offset(20);
        make.left.equalTo(_methodLabel.mas_left);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(15);
    }];
    [_answerField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_answerLabel.mas_bottom).offset(10);
        make.left.equalTo(_answerLabel.mas_left);
        make.width.equalTo(_answerLabel.mas_width);
        make.height.mas_equalTo(28);
    }];
    
    [_answerRequireLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_answerField.mas_bottom).offset(20);
        make.left.equalTo(_answerField.mas_left);
        make.width.equalTo(_answerField.mas_width);
        make.height.mas_equalTo(15);
    }];
    [_answerRequireView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_answerRequireLabel.mas_bottom).offset(10);
        make.left.equalTo(_answerRequireLabel.mas_left);
        make.width.equalTo(_answerRequireLabel.mas_width);
        make.height.mas_equalTo(50);
    }];
}

- (void)settext{
    _methodLabel.text = @"匹配方式:";
    [_fixedButton setTitle:@"固定匹配" forState:UIControlStateNormal];;
    [_dynamicButton setTitle:@"动态匹配" forState:UIControlStateNormal];
    
    [_fixedButton setImage:Image(@"match_normal") forState:UIControlStateNormal];
    [_fixedButton setImage:Image(@"match_selected") forState:UIControlStateSelected];
    
    [_dynamicButton setImage:Image(@"match_normal") forState:UIControlStateNormal];
    [_dynamicButton setImage:Image(@"match_selected") forState:UIControlStateSelected];
    _answerLabel.text = @"答案:";
//    _answerField.placeholder = @"请输入答案";
    //_answerField.text = @"";
    _answerRequireLabel.text = @"答案的要求:";
//    _answerRequireView.text = @"固定匹配：用户提交的答案必须跟您设置的答案一模一样才可以通过！";
    
    UIEdgeInsets insets = UIEdgeInsetsMake(0, 10, 0, 0);
    _dynamicButton.titleEdgeInsets = insets;
    _fixedButton.titleEdgeInsets = insets;
    
    _dynamicButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _fixedButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
}

- (void)setfont{
    _methodLabel.font = FONT(16);
    _fixedButton.titleLabel.font = FONT(16);
    _dynamicButton.titleLabel.font = FONT(16);
    _answerLabel.font = FONT(16);
    _answerField.font = FONT(16);
    _answerRequireLabel.font = FONT(16);
    _answerRequireView.font = FONT(16);
}


- (void)setcolor{
    _methodLabel.textColor = [UIColor colorWithHexString:@"0xff783c"];
    [_fixedButton setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
    [_dynamicButton setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
    _answerLabel.textColor = [UIColor colorWithHexString:@"333333"];
    _answerRequireLabel.textColor = [UIColor colorWithHexString:@"333333"];
    _answerRequireView.textColor = [UIColor colorWithHexString:@"666666"];
    _answerField.textColor = [UIColor colorWithHexString:@"666666"];
    _answerField.backgroundColor = [UIColor colorWithHexString:@"f4f4f4"];
    _answerRequireView.backgroundColor = [UIColor colorWithHexString:@"f4f4f4"];
    
    self.backgroundColor = [UIColor whiteColor];
}

- (UIView *)alphaView{
    if (!_alphaView) {
        _alphaView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _alphaView.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.5];
        _alphaView.hidden = YES;
        
        [[UIApplication sharedApplication].keyWindow addSubview:_alphaView];
        
        UIView *backGround = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_W-50, 210)];
        backGround.backgroundColor = [UIColor whiteColor];
        [backGround.layer setMasksToBounds:YES];
        [backGround.layer setCornerRadius:5];
        backGround.center = [UIApplication sharedApplication].keyWindow.center;
        
        UIButton *button = [[UIButton alloc] init];
        UILabel *label = [[UILabel alloc] init];
        UILabel *fixLabel = [[UILabel alloc] init];
        UILabel *dynamicLabel = [[UILabel alloc] init];
        
        [_alphaView addSubview:backGround];
        
        [backGround addSubview:button];
        [backGround addSubview:label];
        [backGround addSubview:fixLabel];
        [backGround addSubview:dynamicLabel];
        
        [button addTarget:self action:@selector(hiddenAlphaView) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:Image(@"th_home_close") forState:UIControlStateNormal];
        label.font = FONT(16);
        fixLabel.font = FONT(16);
        dynamicLabel.font = FONT(16);
        dynamicLabel.textColor = [UIColor colorWithHexString:@"333333"];
        fixLabel.textColor = [UIColor colorWithHexString:@"0e41dc"];
        label.textColor = [UIColor colorWithHexString:@"333333"];
        label.textAlignment = NSTextAlignmentCenter;
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(backGround.mas_right);
            make.top.equalTo(backGround.mas_top);
            make.height.with.mas_equalTo(34);
        }];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(backGround);
            make.top.equalTo(backGround.mas_top).offset(20);
            make.height.mas_equalTo(15);
        }];
        [fixLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(label.mas_bottom).offset(20);
            make.left.equalTo(backGround.mas_left).offset(18);
            make.right.equalTo(backGround.mas_right).offset(-18);
            make.height.mas_equalTo(52);
        }];
        [dynamicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(fixLabel.mas_bottom).offset(20);
            make.left.equalTo(fixLabel.mas_left);
            make.width.equalTo(fixLabel.mas_width);
            make.height.equalTo(fixLabel.mas_height);
        }];
        
        NSString *fixStr = @"固定匹配：用户提交的答案必须跟您设置的答案一模一样才可以通过！";
        NSString *dynamicStr = @"动态匹配：用户提交的答案包含或等于您设置的答案就可以通过！";
        
        fixLabel.attributedText = [NSString setLineSpaceForString:fixStr lineSpace:12 fontSize:16 andTextColor:[UIColor colorWithHexString:@"ff783c"]];
        
        dynamicLabel.attributedText = [NSString setLineSpaceForString:dynamicStr lineSpace:12 fontSize:16 andTextColor:[UIColor colorWithHexString:@"333333"]];

        label.text = @"匹配方式释义";
        fixLabel.numberOfLines = 2;
        dynamicLabel.numberOfLines = 2;
        
        
        
    }
    return _alphaView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
